//
//  GoodsCategorySubVC.m
//  NormalFramework
//
//  Created by beisheng03 on 2018/3/15.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "GoodsCategorySubVC.h"
#import "GoodsInfoModel.h"
#import "GoodCateSubListCell.h"
#import "GoodsCateModel.h"
@interface GoodsCategorySubVC ()

@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, assign) NSString *pageCount;

/** <#note#> */
@property (nonatomic, strong) GoodsCateModel *parmaModel;

/** <#note#> */
@property (nonatomic, strong) NSMutableArray<GoodsInfoModel *> *dataModels;
@end

@implementation GoodsCategorySubVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GoodCateSubListCell class]) bundle:nil] forCellReuseIdentifier:[GoodCateSubListCell identifierStr]];

    
    MJWeakSelf
    self.tableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        
        [weakSelf loadData];
        
    }];
    
//    [self.tableView.mj_header beginRefreshing];
    
    
    self.tableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
        
    }];
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    
    NSLog(@"%@fdsfdsf",self.title);
    
    
}



- (void)goodsCateoryDidChange:(NSNotification *)noti{
    
    GoodsCateModel *model = noti.object;
    
    
    self.parmaModel = model;
    
    
    [self loadData];
    
    
//    [self.tableView.mj_header beginRefreshing];
  
    
}


- (void)loadData{
    
    
    self.curpage = 1;
    
    NSMutableDictionary *parmars = [NSMutableDictionary dictionary];
    
    parmars[@"curpage"] = @"1";
    
    parmars[@"category_id"] = self.parmaModel.category_id;
    
//    
    
    if ([self.title isEqualToString:@"人气"]) {
        parmars[@"order_type"] = @"1";
    } else if ([self.title isEqualToString:@"最新"]){
        parmars[@"order_type"] = @"2";
    }else if ([self.title isEqualToString:@"进度"]){
        parmars[@"order_type"] = @"3";
    }else{
        //        if (<#condition#>) {
        //            <#statements#>
        //        } else {
        //            <#statements#>
        //        }
        parmars[@"order_type"] = @"4";
        
    }
    
    
    
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"index.php/Api/Dbindex/get_more" parameters:parmars completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            //            NSLog(@"%@",[responseObject mj_JSONString]);
            
            
            
            weakSelf.pageCount = responseObject[@"data"][@"count"];
            
            self.dataModels = [GoodsInfoModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            
            
            [weakSelf.tableView.mj_footer resetNoMoreData];
            
            if ([weakSelf.pageCount isEqualToString:@"1"]) {
                
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                
            }
            
            [weakSelf.tableView.mj_header endRefreshing];
            
            [weakSelf.tableView reloadData];
            
        } else {
            
            [weakSelf.tableView.mj_header endRefreshing];

            [SVProgressHUD showErrorWithStatus:error.domain];
            
        }
        
        
        
    }];
   
    
    
}






- (void)loadMoreData
{
    self.curpage ++;
    
    NSMutableDictionary *parmars = [NSMutableDictionary dictionary];
    
    parmars[@"curpage"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    
    
    if ([self.title isEqualToString:@"人气"]) {
        parmars[@"order_type"] = @"1";
    } else if ([self.title isEqualToString:@"最新"]){
        parmars[@"order_type"] = @"2";
    }else if ([self.title isEqualToString:@"进度"]){
        
        parmars[@"order_type"] = @"3";
        
    }else{
        
        
        //        if (condition
        
     
        
//        ) {
        //            <#statements#>
        //        } else {
        //            <#statements#>
        //        }
        
        
        parmars[@"order_type"] = @"4";
        
    }
    
    
    
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"index.php/Api/Dbindex/get_more" parameters:parmars completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            
            NSMutableArray<GoodsInfoModel *> *temp = [GoodsInfoModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            
            [weakSelf.dataModels addObjectsFromArray:temp];
            
            if (weakSelf.pageCount.integerValue == self.curpage) {
                
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [weakSelf.tableView.mj_footer endRefreshing];
                
                
            }
            
            
            
            [weakSelf.tableView reloadData];
            
        } else {
            
            [weakSelf.tableView.mj_footer endRefreshing];
            
            [SVProgressHUD showErrorWithStatus:error.domain];
            
        }
        
        
        
    }];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.dataModels == nil) {
        return 0;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataModels == nil) {
        return 0;
    }
    return self.dataModels.count;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GoodCateSubListCell *cell = [tableView dequeueReusableCellWithIdentifier:[GoodCateSubListCell identifierStr] ];
    
    cell.model = self.dataModels[indexPath.row];
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 100;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
